package juc;

import org.junit.Test;

import java.util.Random;
import java.util.concurrent.*;

public class JucTest2 {

    @Test
    public void countDownLock() throws InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(3);
        Random random = new Random();
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                //返回[0,5)的值
                int time = random.nextInt(5);
                System.out.println(Thread.currentThread().getName() + " 在路上耗时" + time
                        + "秒");
                try {
                    TimeUnit.SECONDS.sleep(time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " 到达⻋站了");
                countDownLatch.countDown();
            }).start();
        }
        countDownLatch.await();
        System.out.println("⽼司机，发⻋");
    }

    @Test
    public void cyclicBarrier() throws InterruptedException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3, ()->{
            System.out.println("⽼司机，发⻋");
        });
        Random random = new Random();
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                //返回[0,5)的值
                int time = random.nextInt(5);
                System.out.println(Thread.currentThread().getName() + " 在路上耗时" + time
                        + "秒");
                try {
                    TimeUnit.SECONDS.sleep(time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " 到达⻋站了");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        TimeUnit.SECONDS.sleep(2000);
    }

    @Test
    public void completableFuture() throws InterruptedException {
        Random random = new Random();
        CompletableFuture future1 = CompletableFuture.runAsync(() -> {
            int time = random.nextInt(5);
            System.out.println(Thread.currentThread().getName() + " 在路上耗时" +
                    time + "秒");
            try {
                TimeUnit.SECONDS.sleep(random.nextInt());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " 到达⻋站了");
        });
        CompletableFuture future2 = CompletableFuture.runAsync(() -> {
            int time = random.nextInt(5);
            System.out.println(Thread.currentThread().getName() + " 在路上耗时" +
                    time + "秒");
            try {
                TimeUnit.SECONDS.sleep(random.nextInt());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " 到达⻋站了");
        });
        CompletableFuture.allOf(future1, future2).thenRun(() -> {
            System.out.println("⽼司机，发⻋");
        });
        TimeUnit.SECONDS.sleep(2000);
    }
}
